package com.example.user.controller;
import com.example.common.Dto;
import com.example.common.DtoUtil;
import com.example.pojo.GdUser;
import com.example.user.service.RegisterService;
import com.example.utils.Constants;
import com.example.utils.MD5;
import com.example.vo.UserVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import static com.example.utils.Constants.User_AUTH.AUTH_UNKNOWN;

/**
 * Created by LENOVO on 2018/8/17.
 */
@Controller
@RequestMapping("/api")
public class RegisterController {
     @Resource
     private RegisterService registerService;
    /**
     * 手机注册
     * @param userVO
     * @return
     */
    @ApiOperation(value="手机注册功能API",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用手机注册")
    @RequestMapping(value = "/registerbyphone",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto doregisterByPhone(@ApiParam(name="itripUserVO",value="用户信息",required=true) UserVO userVO)
    {
        String phoneReg="^1[3578]{1}\\d{9}$";
        if(userVO.getUserCode().matches(phoneReg))
        {
            try {
                if(!registerService.findUserByUserCode(userVO.getUserCode()))
                 {
                    GdUser user=new GdUser();
                    user.setUsercode(userVO.getUserCode());
                    user.setUsername(userVO.getUserName());
                    user.setUserpassword(MD5.getMd5(userVO.getUserPassword(),32));
                    registerService.registerUserByPhone(user);
                    return DtoUtil.returnSuccess("注册成功");
                 }
                else
                 {
                    return DtoUtil.returnFail("用户已存在", Constants.User_AUTH.AUTH_USER_ALREADY_EXISTS);
                 }
               }
              catch (Exception e) {
               e.printStackTrace();
                  return DtoUtil.returnFail("注册异常", AUTH_UNKNOWN);
              }
        }
        else
        {
            return DtoUtil.returnFail("请输入正确手机号", Constants.User_AUTH.AUTH_ILLEGAL_USERCODE);
        }
    }

    /**
     * 手机短信验证
     * @param user
     * @param code
     * @return
     */
    @ApiOperation(value="手机短信验证API",httpMethod = "PUT", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用手机短信验证")
    @RequestMapping(value = "/validatephone",method = RequestMethod.PUT,produces = "application/json")
    @ResponseBody
    public Dto dovalidateSms(@ApiParam(name="user",value="用户名",required=true)@RequestParam String user,@ApiParam(name="code",value="短信验证码",required=true)@RequestParam String code){
        try {
            if(registerService.validateSms(user,code))
            {
                return DtoUtil.returnSuccess("验证成功");
            }
            else
            {
                return DtoUtil.returnSuccess("验证失败");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("验证失败",AUTH_UNKNOWN);
        }
    }

    /**
     * 邮箱注册
     * @param userVO
     * @return
     * @throws Exception
     */
    @ApiOperation(value="邮箱注册API",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用邮箱注册")
    @RequestMapping(value = "/doregister",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto doregisterByMail(@ApiParam(name="itripUserVO",value="用户信息",required=true)UserVO userVO) throws Exception {

        String mailRege="^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        if(userVO.getUserCode().matches(mailRege))
        {
            try {
                if(!registerService.findUserByUserCode(userVO.getUserCode()))
                {
                    GdUser user=new GdUser();
                    user.setUsercode(userVO.getUserCode());
                    user.setUsername(userVO.getUserName());
                    user.setUserpassword(MD5.getMd5(userVO.getUserPassword(),32));
                    registerService.registerUserByMail(user);
                    return DtoUtil.returnSuccess("注册成功");
                }
                else
                {
                    return DtoUtil.returnFail("用户已存在", AUTH_UNKNOWN);
                }
            }
            catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnSuccess("注册成功");
            }

        }
        else
        {
            return DtoUtil.returnFail("请输入正确邮箱", Constants.User_AUTH.AUTH_ILLEGAL_USERCODE);
        }
    }

    /**
     * 邮箱激活码验证
     * @param user
     * @param code
     * @return
     */
    @ApiOperation(value="邮箱激活码验证API",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用邮箱激活码验证")
    @RequestMapping(value = "/activate",method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto dovActivateMail(@ApiParam(name="user",value="用户名",required=true)@RequestParam String user,@ApiParam(name="code",value="邮箱激活码",required=true)@RequestParam String code){

        try {
            if(registerService.activateMail(user,code))
            {
                return DtoUtil.returnSuccess("激活成功");
            }
            else
            {
                return DtoUtil.returnSuccess("激活失败");
            }
         }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("激活失败",AUTH_UNKNOWN);
        }
    }
}
